package librus.codebase.util;

import java.io.File;

import librus.codebase.packers.IPacker;
import librus.codebase.packers.Unrar;
import librus.codebase.packers.Unzip;

public class FileUtil {
	
	public static String CBR = "cbr";
	public static String CBZ = "cbz";
	public static String RAR = "rar";
	public static String ZIP = "zip";
	
	public static String JPG = "jpg";
	public static String JPEG = "jpeg";
	public static String GIF = "gif";
	public static String BMP = "bmp";
	public static String PNG = "png";
	
	
	public static String getFileExtension(File f)
	{
		String ext = getExtensionFromName(f.getPath());		
		return ext;	
	}
	
	public static Boolean isExtension(File f, String ext)
	{
		Boolean isExt =getFileExtension(f).equals(ext); 
		return isExt;
	}
	
	public static Boolean isComicArchieve(File f)
	{
		return isExtension(f, CBR) || isExtension(f, CBZ) || isExtension(f, RAR) || isExtension(f, ZIP);
	}
	
	public static IPacker getPacker(File f)
	{
		IPacker packer = null;
		
		if (isExtension(f, CBR) || isExtension(f, RAR))
			packer = new Unrar();		
		else if (isExtension(f, CBZ) || isExtension(f, ZIP))
			packer = new Unzip();
		
		return packer;
	}
	
	public static String getExtensionFromName(String name)
	{		
		String ext = name.substring(name.lastIndexOf(".")+1).toLowerCase();
		return ext;
	}
	
	public static Boolean equalsExtension(String name, String ext)
	{
		return getExtensionFromName(name).equals(ext);
	}
	
	public static Boolean isComicFile(String name)
	{
		return equalsExtension(name, BMP) || 
				equalsExtension(name, GIF) || 
				equalsExtension(name, JPEG) || 
				equalsExtension(name, JPG) ||
				equalsExtension(name, PNG);
	}
}
